package com.jinfang.bankpractice.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jinfang.bankpractice.dto.ReturnData;
import com.jinfang.bankpractice.model.User;
import com.jinfang.bankpractice.service.HistoryService;
import com.jinfang.bankpractice.service.RecordService;
import com.wordnik.swagger.annotations.ApiOperation;

@RestController
public class HistoryController {

	private Logger log = LoggerFactory.getLogger(HistoryController.class);
	
	@Autowired
	private RecordService recordService;

	@Autowired
	private HistoryService historyService;

	@ApiOperation(value = "获取所有的历史成绩", notes = "需要登录")
	@RequestMapping(value = "/api/marks", method = RequestMethod.GET)
	public Object all(@RequestParam("sessionid") String sessionid, HttpSession session,@RequestParam("course")int course) {
		User user=(User) session.getAttribute(sessionid);	
    	if(user==null){
    		return new ReturnData(ReturnData.UNAUTHORIZED, "请重新登录", "用户未找到", null);
    	}
    	log.info("get userid from session,userid={}",user.getId());
    	Map<String, Object> map=new HashMap<>();
    	map.put("count", recordService.getHiscount(user.getId(), course));
    	map.put("his", historyService.queryAll(user.getId()).stream()
    			.filter(s->s.getCourse()==course)
    			.sorted((a,b)->
    				a.getCreatetime().compareTo(b.getCreatetime())
    			)
    			.collect(Collectors.toList()));
		return map;
	}
	
	

}
